package com.googlecode.mgwt.examples.uibinder.client;

import com.google.gwt.place.shared.PlaceController;
import com.google.gwt.safehtml.shared.SafeHtmlUtils;
import com.google.web.bindery.event.shared.EventBus;
import com.google.web.bindery.event.shared.SimpleEventBus;
import com.googlecode.mgwt.examples.uibinder.client.activities.ExampleDisplay;
import com.googlecode.mgwt.examples.uibinder.client.activities.ExampleListDisplay;
import com.googlecode.mgwt.examples.uibinder.client.activities.ExampleListDisplayGwtImpl;
import com.googlecode.mgwt.examples.uibinder.client.activities.ExampleSourceDisplay;
import com.googlecode.mgwt.examples.uibinder.client.activities.ExampleSourceDisplayGwtImpl;
import com.googlecode.mgwt.examples.uibinder.client.activities.examples.ExampleDisplayButtonGwtImpl;
import com.googlecode.mgwt.examples.uibinder.client.activities.examples.ExampleDisplayCellListGwtImpl;
import com.googlecode.mgwt.examples.uibinder.client.activities.examples.ExampleDisplayHeaderPanelGwtImpl;
import com.googlecode.mgwt.examples.uibinder.client.activities.examples.ExampleDisplayInputGwtImpl;
import com.googlecode.mgwt.examples.uibinder.client.activities.examples.ExampleDisplaySliderGwtImpl;
import com.googlecode.mgwt.examples.uibinder.client.places.ExamplePlace.ID;

public class ClientFactoryGwtImpl implements ClientFactory {

	private PlaceController placeController;
	private SimpleEventBus eventBus;
	private ExampleListDisplay exampleListDisplay;
	private ExampleDisplayHeaderPanelGwtImpl headerPanelDisplay;
	private ExampleSourceDisplayGwtImpl sourceDisplay;
	private ExampleDisplayButtonGwtImpl buttonDisplay;
	private ExampleDisplayCellListGwtImpl cellListDisplay;
	private ExampleDisplayInputGwtImpl inputDisplay;
	private ExampleDisplay sliderDisplay;

	@Override
	public PlaceController getPlaceController() {
		if (placeController == null) {
			placeController = new PlaceController(getEventBus());
		}
		return placeController;
	}

	@Override
	public EventBus getEventBus() {
		if (eventBus == null) {
			eventBus = new SimpleEventBus();
		}
		return eventBus;
	}

	@Override
	public ExampleListDisplay getExampleListDisplay() {
		if (exampleListDisplay == null) {
			exampleListDisplay = new ExampleListDisplayGwtImpl();
		}
		return exampleListDisplay;
	}

	@Override
	public ExampleDisplay getHeaderPanelDisplay() {
		if (headerPanelDisplay == null) {
			headerPanelDisplay = new ExampleDisplayHeaderPanelGwtImpl();
		}
		return headerPanelDisplay;
	}

	@Override
	public String getSource(ID id) {
		String src = "";
		switch (id) {
		case HEADER_PANEL:
			src = AppBundle.INSTANCE.headerPanelXml().getText();
			break;
		case BUTTON:
			src = AppBundle.INSTANCE.buttonXml().getText();
			break;
		case CELL_LIST:
			src = AppBundle.INSTANCE.cellListXml().getText();
			break;
		case INPUT:
			src = AppBundle.INSTANCE.inputXml().getText();
			break;
		case SLIDER:
			src = AppBundle.INSTANCE.sliderXml().getText();
			break;
		}

		return "<pre>" + SafeHtmlUtils.htmlEscape(src) + "</pre>";

	}

	@Override
	public ExampleSourceDisplay getExampleSourceDisplay() {
		if (sourceDisplay == null) {
			sourceDisplay = new ExampleSourceDisplayGwtImpl();
		}
		return sourceDisplay;
	}

	@Override
	public ExampleDisplay getButtonDisplay() {
		if (buttonDisplay == null) {
			buttonDisplay = new ExampleDisplayButtonGwtImpl();
		}
		return buttonDisplay;
	}

	@Override
	public ExampleDisplay getCellListDisplay() {
		if (cellListDisplay == null) {
			cellListDisplay = new ExampleDisplayCellListGwtImpl();
		}
		return cellListDisplay;
	}

	@Override
	public ExampleDisplay getInputDisplay() {
		if (inputDisplay == null) {
			inputDisplay = new ExampleDisplayInputGwtImpl();
		}
		return inputDisplay;
	}

	@Override
	public ExampleDisplay getSliderDisplay() {
		if (sliderDisplay == null) {
			sliderDisplay = new ExampleDisplaySliderGwtImpl();
		}
		return sliderDisplay;
	}

}
